package com.huizhonglm.diap.ui.tab;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.user.IntegralResponse;
import com.huizhonglm.diap.core.image.Thumb;
import com.huizhonglm.diap.core.storage.model.UserInfo;
import com.huizhonglm.diap.core.utils.LogUtil;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.presenters.tab.TabhostMePresenter;
import com.huizhonglm.diap.ui.BaseFragment;
import com.huizhonglm.diap.ui.app.SettingActivity;
import com.huizhonglm.diap.ui.tab.iView.TabhostMeIView;
import com.huizhonglm.diap.ui.user.ClaimShopActivity;
import com.huizhonglm.diap.ui.user.LoginActivity;
import com.huizhonglm.diap.ui.user.MyCollectionActivity;
import com.huizhonglm.diap.ui.user.MyWalletActivity;
import com.huizhonglm.diap.ui.user.OrderListActivity;
import com.huizhonglm.diap.ui.user.RcmCashBackActivity;
import com.huizhonglm.diap.ui.user.UserInfoActivity;
import com.huizhonglm.diap.widget.RoundImageView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-01 23:27
 */
public class TabhostMeFragment extends BaseFragment implements View.OnClickListener, TabhostMeIView {

    private View rootView;
    private RoundImageView userIcon;
    private TextView nickname;
    private TextView integral;

    private boolean isVisible = false;
    private boolean isPrepared = false;

    private TabhostMePresenter mPresenter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mPresenter = new TabhostMePresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_tabhost_me, container, false);
        }

        View userInfo = rootView.findViewById(R.id.user_info_layout);
        userIcon = (RoundImageView) rootView.findViewById(R.id.user_icon_rimg);
        nickname = (TextView) rootView.findViewById(R.id.nickname_txt);
        integral = (TextView) rootView.findViewById(R.id.integral_txt);
        View allOrder = rootView.findViewById(R.id.all_orders_txt);
        View payOrder = rootView.findViewById(R.id.pay_orders_txt);
        View cplOrder = rootView.findViewById(R.id.cpl_orders_txt);
        View myProperty = rootView.findViewById(R.id.my_property_txt);
        View myCollection = rootView.findViewById(R.id.my_collection_txt);
        View rcmCashBack = rootView.findViewById(R.id.rcm_cash_back_txt);
        View claimShop = rootView.findViewById(R.id.claim_shop_txt);
        View appSetting = rootView.findViewById(R.id.app_setting_txt);

        userInfo.setOnClickListener(this);
        allOrder.setOnClickListener(this);
        payOrder.setOnClickListener(this);
        cplOrder.setOnClickListener(this);
        myProperty.setOnClickListener(this);
        myCollection.setOnClickListener(this);
        rcmCashBack.setOnClickListener(this);
        claimShop.setOnClickListener(this);
        appSetting.setOnClickListener(this);

        isPrepared = true;
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        setUserInfo();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtil.print("tabhostme---setUserVisibleHint");
        boolean userVisibleHint = getUserVisibleHint();
        if (userVisibleHint) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
        }
    }

    @Override
    public void onVisible() {
        if (isPrepared && isVisible) {
            setUserInfo();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.user_info_layout:
                startAction(UserInfoActivity.class);
                break;
            case R.id.all_orders_txt:
                startOrdersPage("A");
                break;
            case R.id.pay_orders_txt:
                startOrdersPage("P");
                break;
            case R.id.cpl_orders_txt:
                startOrdersPage("C");
                break;
            case R.id.my_collection_txt:
                startAction(MyCollectionActivity.class);
                break;
            case R.id.rcm_cash_back_txt:
                startAction(RcmCashBackActivity.class);
                break;
            case R.id.claim_shop_txt:
                startAction(ClaimShopActivity.class);
                break;
            case R.id.my_property_txt:
                startAction(MyWalletActivity.class);
                break;
            case R.id.app_setting_txt:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
        }
    }

    private void setUserInfo(){
        //设置用户信息
        if (UserUtil.isLogin()) {
            UserInfo user = UserUtil.getUserInfo();
            if (userIcon != null) {
                Thumb.getInstance().display(userIcon, user.getUserIcon(), R.drawable.def_icon);
            }
            if (nickname != null) {
                nickname.setText(user.getNickName());
            }
            mPresenter.refreshIntegral(UserUtil.getUserId());
        } else {
            if (userIcon != null) {
                userIcon.setImageResource(R.drawable.def_icon);
            }
            if (nickname != null) {
                nickname.setText("还未登录");
            }
            if (integral != null) {
                integral.setText("点击立即登录/注册");
            }
        }
    }

    private void startOrdersPage(String orderType){
        if (UserUtil.isLogin()) {
            Intent intent = new Intent(getActivity(), OrderListActivity.class);
            intent.putExtra("orderType", orderType);
            startActivity(intent);
        } else {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        }
    }

    private void startAction(Class<?> cla){
        if (UserUtil.isLogin() && cla != null) {
            startActivity(new Intent(getActivity(), cla));
        } else {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        }
    }

    @Override
    public void onRefIntegralSuccess(IntegralResponse resp) {
        if (resp != null) {
            integral.setText(resp.getCurrIntegral() + "积分，距离vip" + resp.getNextLevel() + "还有" + resp.getNextIntegral() + "个积分");
        } else {
            integral.setText("积分获取失败");
        }
    }

    @Override
    public void onRefIntegralFailure() {
        integral.setText("积分获取失败");
    }
}
